const Router = require('@koa/router')
const jwt = require("jsonwebtoken")
const { GoodsModel } = require('../databases/index')
const router = new Router()
router.get("/getAllGoods", async(ctx) => {
    const goods = await GoodsModel.findAll();
    ctx.body = {
        code: 200,
        msg: "success",
        data: {
            goods
        }
    }
})
router.get("/gettypeGoods", async(ctx) => {
    const { typedata } = ctx.request.query;
    console.log("类型",typedata)
    const goods = await GoodsModel.findAll({
        where:{type:typedata}
    });
    if(goods){
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                goods
            }
        }
    }
    
})
router.get("/getGoods", async(ctx) => {
    const { currentPage, pageSize } = ctx.request.query;
    // console.log( currentPage,pageSize)
    offset = (currentPage - 1) * pageSize || 0;
    const result = await GoodsModel.findAndCountAll({
        offset,
        limit: parseInt(pageSize),
    });
    if (result) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                result
            }
        }
    }

})
router.post("/search", async(ctx) => {
    const { Op } = require("sequelize");
    const { value } = ctx.request.body;
    console.log("得到的搜索值", value);
    const datas = await GoodsModel.findAll({
        where: {
            [Op.or]: [{
                    goodsname: {
                        [Op.like]: '%' + value + '%'
                    }
                },
                {
                    goodsID: {
                        [Op.like]: '%' + value + '%'
                    }
                }
            ]
        }
    })
    if (datas) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "success",
                datas
            }
        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "错误",
            }
        }
    }
})
router.post("/deleteGoodsById", async(ctx) => {
    const goodsID = ctx.request.body;
    console.log("选择的用户ID", goodsID.goodsID);
    const goodsid = await GoodsModel.destroy({ where: { goodsID: goodsID.goodsID } })
    if (goodsid) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "goodssid success",
                goodsid
            }
        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "用户不存在"
            }
        }
    }
})
router.post("/deleteGoods", async(ctx) => {
    const selectGoods  = ctx.request.body;
    console.log(selectGoods.H);
    // const select = await GoodsModel.findAll();
    const select = await GoodsModel.destroy({ where: { goodsID: selectGoods.H } });
    console.log('详情', select)
    if (select) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "success",

            }

        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "失败"
            }
        }
    }
})
router.post("/addGoods", async(ctx) => {
    const {addForm } = ctx.request.body;
    console.log(addForm.goodsname,1111111);
    const dl = await GoodsModel.create({ 
        goodsname: addForm.goodsname, 
        price: addForm.price, 
        type: addForm.type, 
        count: addForm.count, 
        text: addForm.text , 
        goodsimg: addForm.goodsimg , 
        goodslongimg: addForm.goodslongimg  
    })
    if (dl) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "success",
            }
        }
    } else {
        console.log('错误')
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "错误",
            }
        }
    }
})
router.post("/getOneGoods", async(ctx) => {
    const { goodsID } = ctx.request.body;
    console.log("获取的ID", goodsID);
    // const goods = await GoodsModel.findAll();
    const goods = await GoodsModel.findOne({ where: { goodsID: goodsID } });
    console.log('用户详情', goods)
    if (goods) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "getOneGoods success",
                goods
            }

        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "用户不存在"
            }
        }
    }
})
router.post("/updateGoods", async(ctx) => {
    let updataForm = ctx.request.body;
    console.log("得到信息", updataForm);
    const goods = await GoodsModel.update({
        goodsname: updataForm.goodsname, 
        price: updataForm.price, 
        type: updataForm.type, 
        count: updataForm.count, 
        text: updataForm.text , 
        goodsimg: updataForm.goodsimg , 
        goodslongimg: updataForm.goodslongimg
    }, {
        where: {
            goodsID: updataForm.goodsID
        }
    });
    if (goods) {
        ctx.body = {
            code: 200,
            msg: "success",
            
        }
    } else {
        ctx.body = {
            code: 500,
            msg: "fail",
        }
    }
})

module.exports = router